﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.Drawing.Printing;
using WMS_BLL;
using WMS_DATABASE_MYSQL;
using WMS_DATABASE_MYSQL.MappingClass;
using WMS_DATABASE_MYSQL.ModalClass;
using WMS_IBLL;
using WMS_WEBAPI.Controllers.Base;

namespace WMS_WEBAPI.Controllers.Table
{
    /// <summary>
    /// 组合查询资产的所有状态，包括是否在库，最近一次的出库，最近一次的入库，是否报废等，此控制器并没有对应实际的表
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class AssetsStatusController : ControllerBase
    {
        private readonly ILogger<AssetsStatusController> _logger;
        private readonly string controllerName;
        private readonly sqlsugar_assetsstatus _sqlsugar_assetsstatus;
        private readonly IMemoryCache _memoryCache;//内存缓存

        private readonly  IAssetsIBLL _assetsIBLL;
        private readonly ICateGoryIBLL _cateGoryIBLL;
        private readonly IOutboundIBLL _outboundIBLL;
        private readonly IInboundIBLL _inboundIBLL;
        private readonly IScrapIBLL _scrapIBLL;


        public AssetsStatusController(ILogger<AssetsStatusController> logger, IAssetsIBLL assetsIBLL, ICateGoryIBLL cateGoryIBLL, IOutboundIBLL outboundIBLL, IInboundIBLL inboundIBLL,IScrapIBLL scrapIBLL, sqlsugar_assetsstatus sqlsugar_assetsstatus, IMemoryCache memoryCache)
        {
            _logger = logger;
            controllerName = GetType().Name;
            _assetsIBLL = assetsIBLL;
            _cateGoryIBLL = cateGoryIBLL;
            _outboundIBLL = outboundIBLL;
            _inboundIBLL = inboundIBLL;
            _scrapIBLL = scrapIBLL;
            _sqlsugar_assetsstatus = sqlsugar_assetsstatus;
            _memoryCache = memoryCache;
        }
        //查询所有物资的在库状态
        //[Authorize(Roles = "superadmin,admin")]
        [HttpGet]
        public async Task<IActionResult> GetAssetsStatusAsync(int current, int pageSize, int? assetsclass, int? assetsname,string? sn,string? assetsnumber, byte? lastoutboundtype,string? status,string? borrower,string?project, string? vehicle)
        {
            var ordata =await _memoryCache.GetOrCreateAsync<List<AssetStatusDto>>("assetsstatusdata", async value =>
            {
                return await _sqlsugar_assetsstatus.GetAssetStatusesAsync();
            });
            //var data = await _sqlsugar_assetsstatus.GetAssetStatusesAsync();
            var searchdata= ordata?.Where(a => (assetsclass == null || a.assetsclass == assetsclass) && (assetsname == null || a.assetsname == assetsname)
            && (sn == null || a.sn == sn) && (assetsnumber == null || a.assetsnumber == assetsnumber) && (lastoutboundtype == null || a.lastoutboundtype == lastoutboundtype) && (status == null || a.status == status) && (borrower == null || a.borrower == borrower) && (project == null || a.project == project) && (vehicle == null || a.vehicle == vehicle));
            var data= searchdata?.Skip(pageSize * (current - 1)).Take(pageSize).ToList();
            var count= data?.Count();
            return Ok(new { data, total = count, suceess = true });
        }
    }
}
